package main

import (
	"fmt"
	"github.com/caarlos0/env/v6"
	restfulspec "github.com/emicklei/go-restful-openapi/v2"
	"github.com/emicklei/go-restful/v3"
	"go-code-frame-demo/api"
	"go-code-frame-demo/configs"
	"log"
	"net/http"
	"syscall"
)

var CFG *configs.Config

func init() {
	cfg := configs.Config{}
	if err := env.Parse(&cfg); err != nil {
		fmt.Println(err)
		syscall.Exit(1)
	}
	CFG = &cfg
}

func main() {
	u := api.UserResource{Users: map[string]api.User{}}
	restful.DefaultContainer.Add(u.WebService())

	config := restfulspec.Config{
		WebServices:                   restful.RegisteredWebServices(), // you control what services are visible
		APIPath:                       "/apidocs.json",
		PostBuildSwaggerObjectHandler: api.EnrichSwaggerObject}
	restful.DefaultContainer.Add(restfulspec.NewOpenAPIService(config))

	// Optionally, you can install the Swagger Service which provides a nice Web UI on your REST API
	// You need to download the Swagger HTML5 assets and change the FilePath location in the config below.
	// Open http://localhost:8080/apidocs/?url=http://localhost:8080/apidocs.json
	http.Handle("/apidocs/", http.StripPrefix("/apidocs/", http.FileServer(http.Dir("/Users/emicklei/Projects/swagger-ui/dist"))))

	log.Printf("start listening on localhost:8080")
	log.Fatal(http.ListenAndServe(":8080", nil))
}
